package com.zoe.onelink.governance.gateway.route.generator;

import com.zoe.onelink.governance.gateway.model.NativeGatewayRoute;
import com.zoe.onelink.governance.gateway.property.NativeGatewayDynamicRouteProperties;
import com.zoe.onelink.governance.gateway.route.GatewayDynamicRouteGenerator;
import com.zoe.onelink.governance.property.OnelinkGovernanceProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.Environment;

import java.util.Collections;
import java.util.List;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2022</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2022-10-20
 */
@RequiredArgsConstructor
public class NativeGatewayDynamicRouteGenerator implements GatewayDynamicRouteGenerator {

    private final OnelinkGovernanceProperties onelinkGovernanceProperties;
    private final Environment environment;

    @Override
    public List<NativeGatewayRoute> generateNativeRoutes(String serviceName) {
        NativeGatewayDynamicRouteProperties nativeGatewayDynamicRouteProperties = Binder.get(this.environment)
                .bindOrCreate(onelinkGovernanceProperties.getGateway().getDynamicRoutePublish().getPrefix(), NativeGatewayDynamicRouteProperties.class);
        return nativeGatewayDynamicRouteProperties.getRoutes() == null ? Collections.emptyList() : nativeGatewayDynamicRouteProperties.getRoutes();
    }
}
